/*******************************************************************************
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.moo2u2.android.oaktononthego.server;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.persistence.EntityManager;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Colleague {

    private static final Logger log = Logger.getLogger(Colleague.class
            .getName());

    private static final Integer VERSION = 1;

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    Long id;

    @Persistent
    String name;

    @Persistent
    String snippet;

    @Persistent
    Integer longitude;

    @Persistent
    Integer latitude;

    @Persistent
    String phone;

    @Persistent
    String img;

    public Colleague() {

    }

    public Colleague(String name, String snippet) {
        this.name = name;
        this.snippet = snippet;
    }

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getSnippet() {
        return snippet;
    }

    public Integer getLongitude() {
        return longitude;
    }

    public void setLongitude(Integer longitude) {
        this.longitude = longitude;
    }

    public Integer getLatitude() {
        return latitude;
    }

    public void setLatitude(Integer latitude) {
        this.latitude = latitude;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public static final EntityManager entityManager() {
        return EMF.get().createEntityManager();
    }

    public static Colleague findColleague(Long id) {
        if (id == null) {
            return null;
        }
        // EntityManager em = entityManager();
        // try {
        // Colleague employee = em.find(Colleague.class, id);
        // return employee;
        // } finally {
        // em.close();
        // }
        Colleague found = null;
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            found = pm.getObjectById(Colleague.class, id);
        } catch (Exception e) {
            log.severe("Couldn't locate colleague by id: " + id);
        } finally {
            pm.close();
        }
        return found;
    }

    @SuppressWarnings("unchecked")
    public static List<Colleague> findColleagues() {
        // EntityManager em = entityManager();
        // try {
        // List<Colleague> list = em.createQuery("select o from Colleague o")
        // .getResultList();
        // list.size();
        // log.info("Retrieving " + list.size() + " colleagues");
        // return list;
        // } finally {
        // em.close();
        // }

        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            Query query = pm.newQuery(Colleague.class);
            List<Colleague> qresult = (List<Colleague>) query.execute();
            List<Colleague> result = new ArrayList<Colleague>();
            for (Colleague di : qresult) {
                result.add(di);
            }
            query.closeAll();
            log.info("Return " + result.size() + " colleagues");
            return result;
        } finally {
            pm.close();
        }
    }

    public void persist() {
        // EntityManager em = entityManager();
        // try {
        // em.persist(this);
        // } finally {
        // em.close();
        // }

        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(this);
        } catch (Exception e) {
            log.severe("Couldn't persist colleague: " + e);
        } finally {

            pm.close();
        }
    }

    public void remove() {
        // EntityManager em = entityManager();
        // try {
        // Colleague attached = em.find(Colleague.class, this.id);
        // em.remove(attached);
        // } finally {
        // em.close();
        // }

        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.deletePersistent(this);
        } catch (Exception e) {
            log.severe("Couldn't remove colleague: " + e);
        } finally {
            pm.close();
        }

    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSnippet(String snippet) {
        this.snippet = snippet;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getVersion() {
        return VERSION;
    }

    @Override
    public String toString() {
        return "Colleage [name=" + name + ", snippet=" + snippet + "]";
    }

    public Node toXmlNode(Document d) {
        Element eltItem = d.createElement("colleague");

        Element eltName = d.createElement("name");
        eltName.appendChild(d.createTextNode(name));
        eltItem.appendChild(eltName);

        Element eltDescription = d.createElement("snippet");
        eltDescription.appendChild(d.createTextNode(snippet));
        eltItem.appendChild(eltDescription);

        Element eltLatitude = d.createElement("latitude");
        eltLatitude.appendChild(d.createTextNode(String.valueOf(latitude)));
        eltItem.appendChild(eltLatitude);

        Element eltLongitude = d.createElement("longitude");
        eltLongitude.appendChild(d.createTextNode(String.valueOf(longitude)));
        eltItem.appendChild(eltLongitude);

        Element eltPhone = d.createElement("phone");
        eltPhone.appendChild(d.createTextNode(phone));
        eltItem.appendChild(eltPhone);

        Element eltImg = d.createElement("img");
        eltImg.appendChild(d.createTextNode(img));
        eltItem.appendChild(eltImg);

        return eltItem;
    }
}
